function sac3_summary()
% make a summary figure for the sac3 analysis...
% 1/10/2007 - difference code added back in...

global CONTROL
dispit = 1; % flag - print data to a file
norm = 1; % flag  - normalize the data over refpts window
difference = 0; % flag - take difference of data between a control stim and one with the IPSP inserted
% note: either disp or norm should be set - never both at the same time.

refpts = 2:5; % normalization window - 2nd to 5th points in this case
t=50:100:950; % time base reconstructed here.
[x, f] = getstructs('SAC3.NPH1');

nsp = getstructs('SAC3.ns'); % get spike count

fu = unique(f); % find unique filenames
% now we match...
% sstim = {'I', 'NI', 'NI2'}; % protocols to look for in the database
sstim = {'E'};
tns = [];
for i = 1:length(sstim)
    tns=[tns find(strcmp(sstim{i}, {CONTROL.E_C}) ~= 0)];
end; % tni now contains a list of all those protocols.

flat = {'Noise', 'NF2', 'N'};
tnf = [];
for i = 1:length(flat)
    tnf=[tnf find(strcmp(flat{i}, {CONTROL.E_C}) ~= 0)];
end; % tnf now contains list of all THOSE protocols


if(length(tns) ~= length(tnf))
    fprintf(1, 'Pairing is not exact: %d w/stim, %d w/nostim\n', tns, tnf);
end; % just let the user know - we don't do anything about it though

sel = getmainselection; % get selected list in database.

% now we are going to build an index of all of the pairs.
% jp holds the index. columns 3 and 4 have the database tag (index)
% to the two protocols that we have selected from above...
jp=[];
kk  = 1;
for k = 1:length(sel)
    fn=CONTROL(sel(k)).filename; % filenames selected
    tb=strmatch(fn, {CONTROL.filename}); % which one is ours?
    ns = sort(intersect(intersect(tb, tns), sel)); % get protocols run under that file
    nf = sort(intersect(intersect(tb, tnf), sel)); % but just those in the selection list
    %    ne = sort(intersect(tb, tne));
    if(length(ns) == 1 && length(nf) == 1 ) % must be an entry to build index
        if(isempty(jp) || isempty(intersect(ns, jp(:,1))))
            jp(kk,1)=ns; % protocols with our file.
            jp(kk,2)=nf; % index to selection
            %           jp(kk,3)=ne;
            jp(kk,3)=find(sel == ns); % current selection in order (matches Y) (index is into "sel")
            jp(kk,4)=find(sel == nf); % linked selection in order (matches Y)
            kk = kk + 1;
        end;
    else
        fprintf(1, 'Multiple matches ... in %s\n',fn); % if multiple matches, we skip the cell
        % logical thing is to make the user identify which go together
        % (unique protocols?)
    end;
end;

Y = reshape([x{:}], length(t), length(x)); % make x into a two-dimensional array
Ys = Y(:, jp(:,3)); % experimental manipulation
Yn = Y(:, jp(:,4)); % control (reference);
if(norm)
    mvs=mean_nonan(Ys(refpts, :)')';
    mvn=mean_nonan(Yn(refpts, :)')';
    for i = 1:length(mvs)
        Ys(:,i) = Ys(:,i) ./ mvs(i);
        Yn(:,i) = Yn(:,i) ./ mvn(i);
    end;

end;

%     j = 1;
%     for i = 1:2:size(Y,2)
%             Y1(:,j) = Y(:,i) / mean_nonan(Y(refpts, i)'); % normalized by mean over a windows
%         Y2(:,j) = Y(:,i+1) / mean_nonan(Y(refpts, i+1)');
%         j = j + 1;
%     end;
% else
%     j = 1;
%     for i = 1:2:size(Y,2)
%         Y1(:,j) = Y(:,i); % normalized by mean over a windows
%         Y2(:,j) = Y(:,i+1);
%         j = j + 1;
%     end;
% end;

[ms, ss, ns] = mean_nonan(Ys); % get mean, std, etc. using our routine that deals with NaNs.
[mn, sn, nn] = mean_nonan(Yn); % get mean, std, etc. using our routine that deals with NaNs.

if(difference)
    Yd=[];
%    kk = 1;
     Yd = Y(:, jp(:,3)) - Y(:, jp(:,4));
%     for i = 1:size(jp, 1)
%         Yd(:,kk) = Y(:,jp(i,3)) - Y(:, jp(i,4)); % subtract directly between the two measures, pre normalization
%         kk = kk + 1;
%     end;
    [md, sd, nd] = mean_nonan(Yd);
end;



newfigure('sac3summaryCI', sprintf('Sac3 Summary Plot CI, N=%d', max(nn)));
t=50:100:950;
subplot(2,1,1);
plot_witherror(t(1:end-1), mn(1:end-1), sn(1:end-1)./sqrt(nn(1:end-1)), refpts, 'CI', 100, 'o', 'k');
%set(gca, 'Yscale', 'log');
hold on
plot_witherror(t(1:end-1), ms(1:end-1), ss(1:end-1)./sqrt(ns(1:end-1)), refpts, 'CI', 100, 's', 'r');
hold on
%plot(t(1:end-1), Yc(1:end-1,:), 'kx-', 'markersize', 2.0, 'markeredgecolor', 'k', 'markerfacecolor', 'k');
%plot(t(1:end-1), Ye(1:end-1,:), 'ro-', 'markersize', 2.0, 'markeredgecolor', 'r', 'markerfacecolor', 'r');
% % % % if(difference)
% % % %     set(gca, 'Ylim', [-20 100]);
% % % % else
% % % %     set(gca, 'Ylim', [0 50]);
% % % % end;
if(difference)
    plot_witherror(t(1:end-1), md(1:end-1), sd(1:end-1)./sqrt(nd(1:end-1)), refpts, 'Normalized difference', 100, '^', 'b');
    set (gca, 'YLim', [0 4]);
    hold on
end;

tl = find(t > 500 & t < 900);
msl = (mean_nonan(Ys(tl, :)));
mnl = (mean_nonan(Yn(tl, :)));
for i = 1:length(msl)
    fprintf(1, 'late window: Mean no stim: %8.3f   mean stim: %8.3f\n', mnl(i), msl(i));
end;
fprintf(1, 'late window   mean:        %8.3f   mean:      %8.3f\n', mean(mnl), mean(msl));
te = find(t > 50 & t < 500);
mne = (mean_nonan(Yn(te, :)));
mse = (mean_nonan(Ys(te, :)));
for i = 1:length(mne)
    fprintf(1, 'Early window: Mean no stim: %8.3f   mean stim: %8.3f\n', mne(i), mse(i));
end;
fprintf(1, 'Early window  mean:         %8.3f   mean:      %8.3f\n', mean(mne), mean(mse));
%
%
% store data off into a file to be read elsehwere (like in Igor).
if(dispit)
    h = fopen('/users/pmanis/desktop/sac3out.txt', 'w');
    for i = 1:length(t)
        if(i == 1) % title line
            fprintf(h, 't,');
            fprintf(h, 'Yn,  Ys');
            fprintf(h, '\n');
        end;
        fprintf(h, '%f,', t(i));
        for j = 1:size(Ys,2)
            fprintf(h, '%f,', Yn(i, j));
            fprintf(h, '%f,', Ys(i, j));

        end;
        fprintf(h, '\n');
    end;
    fclose(h);
end;

% do a plot of the half-widths for comparison. This is easier, since we
% dont' mash the data before doing a plot. At least not yet...
return;

[xh] = getstructs('SAC3.fwhm1');
Yh = reshape([xh{:}], length(t), length(xh));
Yhe = Yh(:, jp(:,3)); % experimental manipulation
Yhc = Yh(:, jp(:,4)); % control (reference);
% if(norm)
%     j = 1;
%     for i = 1:2:size(Yh,2)
%         Yh1(:,j) = Yh(:,i) / mean_nonan(Yh(refpts, i)'); % normalized by mean over a windows
%         Yh2(:,j) = Yh(:,i+1) / mean_nonan(Yh(refpts, i+1)');
%         j = j + 1;
%     end;
% else
%     j = 1;
%     for i = 1:2:size(Yh,2)
%         Yh1(:,j) = Yh(:,i); % normalized by mean over a windows
%         Yh2(:,j) = Yh(:,i+1);
%         j = j + 1;
%     end;
%
% end;

[mhc, shc, nhc] = mean_nonan(Yhc);
[mhe, she, nhe] = mean_nonan(Yhe);

subplot(2,1,2);
plot_witherror(t, mhc, shc./sqrt(nhc), refpts, 'HW (msec)', 100);
hold on
plot_witherror(t, mhe, she./sqrt(nhe), refpts, 'HW (msec)', 100);
hold on
plot(t, Yhc, 'k-', 'markersize', 2.0, 'markeredgecolor', 'k', 'markerfacecolor', 'k');
plot(t, Yhe, 'r-', 'markersize', 2.0, 'markeredgecolor', 'r', 'markerfacecolor', 'r');
set(gca, 'Ylim', [0 10]);
% u=find(Y(6,:) > 15);
% sf=getmainselection;
% sf(u)

